﻿#include"Tree.h"

//前序遍历
void preOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("NULL ");
		return;
	}
	printf("%c ", root->data);
	preOrder(root->left);
	preOrder(root->right);

}

//中序遍历
void InOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("NULL ");
		return;
	}
	InOrder(root->left);
	printf("%c ", root->data);
	InOrder(root->right);

}


//后序遍历
void Postorder(BTNode* root)
{
	if (root == NULL)
	{
		printf("NULL ");
		return;
	}
	Postorder(root->left);
	Postorder(root->right);
	printf("%c ", root->data);
}


//二叉树节点个数---根节点个数（1） + 左子树节点个数 + 右子树节点个数
int BinaryTreeSize(BTNode* root)
{
	if (root == NULL)
		return 0;
	return 1 + BinaryTreeSize(root->left) + BinaryTreeSize(root->right);
}

//二叉树节点个数---遍历
int BinaryTreeSize1(BTNode* root, int* psize)
{
	if (root == NULL)
		return 0;
	(*psize)++;
	BinaryTreeSize1(root->left, psize);
	BinaryTreeSize1(root->right, psize);
	return *psize;
}

//二叉树叶子结点个数---左子树叶子结点个数+右子树叶子结点个数
int BinaryTreeLeafSize(BTNode* root)
{
	if (root == NULL)
		return 0;
	if (root->left == NULL && root->right == NULL)
		return 1;
	return BinaryTreeLeafSize(root->left) + BinaryTreeLeafSize(root->right);

}

// ⼆叉树第k层结点个数----左子树该层的节点个数 + 右子树该层的节点个数
int BinaryTreeLevelKSize(BTNode* root, int k)
{
	if (root == NULL)
		return 0;
	if (k == 1)
		return 1;
	return BinaryTreeLevelKSize(root->left, k - 1) + BinaryTreeLevelKSize(root->right, k - 1);
}


//⼆叉树的深度/⾼度---1 + max（左子树高度， 右子树高度）
int BinaryTreeDepth(BTNode* root)
{
	if (root == NULL)
		return 0;
	int leftDeep = BinaryTreeDepth(root->left);
	int rightDeep = BinaryTreeDepth(root->right);
	return 1 + (leftDeep > rightDeep ? leftDeep : rightDeep);
}


// ⼆叉树查找值为x的结点
BTNode* BinaryTreeFind(BTNode* root, BTDataType x)
{
	if (root == NULL)
		return NULL;
	if (root->data == x)
		return root;
	BTNode* leftFind = BinaryTreeFind(root->left, x);
	if (leftFind)
	{
		return leftFind;
	}
	BTNode* rightFind = BinaryTreeFind(root->right, x);
	if (rightFind)
		return rightFind;
	return NULL;
}


// ⼆叉树销毁
void BinaryTreeDestory(BTNode** root)
{
	if ((*root) == NULL)
		return;
	BinaryTreeDestory(&((*root)->left));
	BinaryTreeDestory(&((*root)->right));
	free(*root);
	*root = NULL;
}